<template>
    <div class="views-pinglunhuifu-updt">
        <div>
            <el-card class="box-card">
                <template #header>
                    <div class="clearfix">
                        <span class="title"> 添加评论回复 </span>
                    </div>
                </template>

                <el-form :model="form" ref="formModel" :label-width="labelWidth" status-icon validate-on-rule-change>
                    <el-form-item v-if="isRead" label="表 " prop="biao" required :rules="[{required:true, message:'请填写表'}]"> {{ form.biao }} </el-form-item>

                    <el-form-item
                        v-if="isRead"
                        label="表id "
                        prop="biaoid"
                        required
                        :rules="[{required:true, message:'请填写表id'}, {validator:rule.checkNumber, message:'输入一个有效数字'}]"
                    >
                        {{form.biaoid}}
                    </el-form-item>

                    <el-form-item v-if="isRead" label="标题 " prop="biaoti" required :rules="[{required:true, message:'请填写标题'}]"> {{ form.biaoti }} </el-form-item>

                    <el-form-item v-if="isRead" label="评论内容 " prop="pinglunneirong"> {{ form.pinglunneirong }} </el-form-item>

                    <el-form-item label="回复内容 " prop="huifuneirong"> <el-input type="textarea" v-model="form.huifuneirong"></el-input> </el-form-item>

                    <el-form-item v-if="btnText">
                        <el-button type="primary" @click="submit">{{ btnText }}</el-button>
                    </el-form-item>
                </el-form></el-card
            >
        </div>
    </div>
</template>

<script setup>
    import http from "@/utils/ajax/http";
    import DB from "@/utils/db";
    import rule from "@/utils/rule";
    import router from "@/router";

    import { ref, reactive, computed, watch } from "vue";
    import { useRoute } from "vue-router";
    import { session } from "@/utils/utils";
    import { ElMessage, ElMessageBox } from "element-plus";
    import { usePinglunhuifuFindById, canPinglunhuifuFindById, canPinglunhuifuUpdate, canPinglunFindById } from "@/module";
    import { extend } from "@/utils/extend";

    const route = useRoute();
    const props = defineProps({
        id: [String, Number],
        btnText: {
            type: String,
            default: "保存",
        },
        isRead: {
            type: Boolean,
            default: true,
        },
        isHouxu: {
            type: Boolean,
            default: true,
        },
        labelWidth: {
            type: String,
            default: "140px",
        },
    });
    const form = usePinglunhuifuFindById(props.id);
    const emit = defineEmits(["success"]);
    const formModel = ref();
    const loading = ref(false);

    const submit = () => {
        formModel.value.validate().then((res) => {
            if (loading.value) return;
            loading.value = true;
            canPinglunhuifuUpdate(form).then(
                (res) => {
                    loading.value = false;
                    if (res.code == 0) {
                        emit("success", res.data);
                        if (props.isHouxu) {
                            ElMessage.success("更新成功");
                            router.go(-1);
                        }
                    } else {
                        ElMessageBox.alert(res.msg);
                    }
                },
                (err) => {
                    loading.value = false;
                    ElMessageBox.alert(err.message);
                }
            );
        });
    };

    const readMap = reactive({});
    watch(
        () => form.pinglunid,
        (id) => {
            canPinglunFindById(id).then((res) => {
                extend(readMap, res);
            });
        }
    );
</script>

<style scoped lang="scss">
    .views-pinglunhuifu-updt {
    }
</style>
